package model.expression.command;


import state.TabState;
import state.TurtleState;


public class Forward extends AbstractCommandExpression {

	public Forward(TabState ts) {
		super(ts);
	}
	
	public int evaluate() {
		// Needs to modify state by the value of its children;
		//TODO: SEEMS NEED TO EVALUATE IN THE LOOP BELOW, CONSIDER RANDOM FUNCTION
		int step = this.children.get(0).evaluate();

		// for each active turtle, evaluate
		for (Integer i : tabSt.getActiveTurtle()) {
			TurtleState st = tabSt.turtlesMap.get(i);
			int[] result = st.calculateCoordinates(step);
			// notify Observers in newCoords
			super.newCoords(result[0], result[1], st);

		}
		return step;
	}

	private int[] calculateCoordinates(int x, int y, int degrees, int length) {
		int[] result = new int[2];
		result[0] = (int) Math.rint(Math.sin(Math.toRadians(degrees)) * length)
				+ x;
		result[1] = (int) Math.rint(Math.cos(Math.toRadians(degrees)) * length)
				+ y;
		return result;

	}

	@Override
	protected int getExpNumChildren() {
		return 1;
	}

}
